package com.pay.cloud.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;

import com.pay.cloud.BindState;
import com.pay.cloud.IconFontFactory;
import com.pay.cloud.R;
import com.pay.cloud.SubScreenFactory;


public abstract class PayCancelAbstract extends TipHorizontalFragment implements BindState {
    private String amount;
    private String orderNo;

    public PayCancelAbstract(String orderNo, String amount) {
        super(TipType.WAIT, R.string.cancel_wait);
        this.orderNo = orderNo;
        this.amount = amount;
    }

    @Override
    public void execute() throws Exception {
        SubScreenFactory.startSubScreen(this::flushSubScreenToCancelIng);
        speak(getString(R.string.cancel_wait));

    }

    @Override
    public boolean useNetwork() {
        return true;
    }

    @Override
    public void onError(String msg) {
        showToast(msg);
        onChange(TipType.ERROR, getString(R.string.cancel_fail));
        super.onError(msg);
    }

    public String getAmount() {
        return amount;
    }

    public String getOrderNo() {
        return orderNo;
    }

    protected void onFail(String msg) {
        showToast(msg);
        msg = getString(R.string.cancel_fail);
        speak(msg);
        onChange(TipType.ERROR, msg);
        SubScreenFactory.startSubScreen(this::flushSubScreenToCancelFail);
    }

    protected void onSuccess() {
        String msg = getString(R.string.cancel_success);
        speak(msg);
        onChange(TipType.SUCCESS, msg);
        SubScreenFactory.startSubScreen(this::flushSubScreenToCancelSuccess);
    }

    private void flushSubScreenToCancelIng() {
        flushSubScreenToPayStatus( R.color.colorWarning, R.string.glyphicon_exclamation_sign, R.string.cancel_wait);
    }

    private void flushSubScreenToCancelSuccess() {
        flushSubScreenToPayStatus(R.color.colorSuccess, R.string.glyphicon_ok_sign, R.string.cancel_success);
    }

    private void flushSubScreenToCancelFail() {
        flushSubScreenToPayStatus(R.color.colorDanger, R.string.glyphicon_remove_sign, R.string.cancel_fail);
    }

    private void flushSubScreenToPayStatus(int colorId, int iconId, int textId) {
        Context context = getContext();
        if (null == context) {
            return;
        }

        Bitmap bitmap = SubScreenFactory.createBitmap(context);

        Canvas canvas = new Canvas(bitmap);
        //canvas.drawColor(Color.WHITE);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        //paint.setFakeBoldText(true);
        paint.setTextSize(80f);

        int moneyWidth = (int) paint.measureText(amount);
        int startX = (bitmap.getWidth() - moneyWidth) / 2;
        canvas.drawText(amount, startX, 170, paint);

        Paint iconPaint = new Paint();
        iconPaint.setAntiAlias(true);
        iconPaint.setTextSize(40f);
        iconPaint.setColor(context.getColor(colorId));
        Typeface typeface = IconFontFactory.getHalflings(context);
        iconPaint.setTypeface(typeface);

        String icon = getString(iconId);
        int iconWidth = (int) iconPaint.measureText(icon);

        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(30f);

        String text = getString(textId);
        int textWidth = (int) textPaint.measureText(text);

        startX = (bitmap.getWidth() - iconWidth - textWidth) / 2;
        canvas.drawText(icon, startX, 230, iconPaint);

        startX = startX + iconWidth + 10;
        canvas.drawText(text, startX, 220, textPaint);

        canvas.save();
        canvas.restore();

        SubScreenFactory.flush(bitmap);

    }

}
